package com.da.lon.wang.xlg.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.PartyInfoAdapter;
import com.da.lon.wang.xlg.bean.PartyFragmentBean.PartyResult.PartyResultList.PartyResultProList;
import com.da.lon.wang.xlg.bean.PartyInfoResultBean;
import com.da.lon.wang.xlg.bean.PartyInfoResultBean.PartyInfoBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshScrollView;
import com.da.lon.wang.xlg.util.DialogUtil;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.Utils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.osell.selection.widget.MyGridView;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class PartyInfoActivity extends BaseActivity implements OnClickListener,
        OnCheckedChangeListener, OnItemClickListener {
    ImageButton party_into_top_left, party_info_top_right;
    TextView party_info_title, party_info_rq, party_info_xl, party_info_xp;
    CheckBox party_info_jg;
    ImageView party_info_img;
    MyGridView party_info_gridview;
    PartyInfoAdapter adapter;
    ArrayList<PartyResultProList> list;
    private DialogUtil dialogUtil;
    private PullToRefreshScrollView tp_scllv;
    private PartyInfoBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.party_info_activity);
        Utils.setBasicUI(this);
        initView();
        party_info_rq.setTextColor(Color.parseColor("#FF3E47"));
        party_info_xl.setTextColor(Color.parseColor("#333333"));
        party_info_xp.setTextColor(Color.parseColor("#333333"));
        party_info_jg.setTextColor(Color.parseColor("#333333"));

        Drawable drawable = getResources().getDrawable(
                R.drawable.ocoffee_search_pic1);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        party_info_jg.setCompoundDrawables(null, null, drawable, null);
        getAndShowData("0");

    }

    /**
     * 获取并显示数据 order 0 人气 1销量 2 新品 3 价格升 4 价格降
     */
    private void getAndShowData(String order) {
        Intent intent = getIntent();
        String activityid = intent.getStringExtra("id");
        String url = XLGConstants.URL + "Activity-detail";
        HttpLoader<PartyInfoResultBean> loder = new HttpLoader<PartyInfoResultBean>(
                this, url, PartyInfoResultBean.class);
        AjaxParams params = new AjaxParams();
        params.put(
                "token",
                XLGDataUtils.encrypt2MD5("Activity" + XLGConstants.SECRET
                        + "detail"));
        params.put("activity_id", activityid);
        params.put("order", order);
        loder.loadData(params);
        loder.setManagerListener(new OnWebLoadListener<PartyInfoResultBean>() {

            @Override
            public void OnStart() {
                dialogUtil.showDialog();
            }

            @Override
            public void OnError(String error) {
                dialogUtil.dismissDialog();
            }

            @Override
            //
            public void onSuccess(PartyInfoResultBean t) {
                dialogUtil.dismissDialog();
                if (t != null && "000000".endsWith(t.getCode())) {
                    bean = t.getResult();
                    if (bean == null) {
                        return;
                    }
                    if (bean.getInfo() != null
                            && bean.getInfo().getName() != null) {
                        party_info_title.setText(bean.getInfo().getName());
//						MyApplication.loadImage(PartyInfoActivity.this, bean
//								.getInfo().getImg_url(), party_info_img);
                        MyApplication.loadImageByPicasso(PartyInfoActivity.this, bean
                                .getInfo().getImg_url(), null, party_info_img, 0, 0, 0);
                    }
                    if (null != bean.getList()) {
                        adapter = new PartyInfoAdapter(PartyInfoActivity.this,
                                bean.getList());
                        party_info_gridview.setAdapter(adapter);
                    }
                }
            }
        });
    }

    /**
     * 控件初始化
     */
    private void initView() {
        dialogUtil = new DialogUtil(this);
        tp_scllv = (PullToRefreshScrollView) findViewById(R.id.tp_scllv);
        tp_scllv.setMode(Mode.DISABLED);
        party_into_top_left = (ImageButton) findViewById(R.id.party_into_top_left);
        party_info_top_right = (ImageButton) findViewById(R.id.party_info_top_right);
        party_info_title = (TextView) findViewById(R.id.party_info_title);
        party_info_rq = (TextView) findViewById(R.id.party_info_rq);
        party_info_xl = (TextView) findViewById(R.id.party_info_xl);
        party_info_xp = (TextView) findViewById(R.id.party_info_xp);
        party_info_jg = (CheckBox) findViewById(R.id.party_info_jg);
        party_info_img = (ImageView) findViewById(R.id.party_info_img);
        party_info_gridview = (MyGridView) findViewById(R.id.party_info_gridview);
        party_into_top_left.setOnClickListener(this);
        party_info_top_right.setOnClickListener(this);
        party_info_rq.setOnClickListener(this);
        party_info_xl.setOnClickListener(this);
        party_info_xp.setOnClickListener(this);
        party_info_jg.setOnCheckedChangeListener(this);
        party_info_img.setOnClickListener(this);
        party_info_gridview.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.party_into_top_left:
                finish();
                break;
            case R.id.party_info_top_right:
                // 分享
                if (bean != null) {
                    Utils.showShare(PartyInfoActivity.this, bean.getInfo().getName(),
                            bean.getInfo().getInfo(), bean.getInfo().getShare_url(),
                            bean.getInfo().getShare_img());
                }
                break;
            case R.id.party_info_rq:
                party_info_rq.setTextColor(Color.parseColor("#FF3E47"));
                party_info_xl.setTextColor(Color.parseColor("#333333"));
                party_info_xp.setTextColor(Color.parseColor("#333333"));
                party_info_jg.setTextColor(Color.parseColor("#333333"));

                Drawable drawable = getResources().getDrawable(
                        R.drawable.ocoffee_search_pic1);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                        drawable.getMinimumHeight());
                party_info_jg.setCompoundDrawables(null, null, drawable, null);

                getAndShowData("0");
                break;

            case R.id.party_info_xl:

                party_info_rq.setTextColor(Color.parseColor("#333333"));
                party_info_xl.setTextColor(Color.parseColor("#FF3E47"));
                party_info_xp.setTextColor(Color.parseColor("#333333"));
                party_info_jg.setTextColor(Color.parseColor("#333333"));

                Drawable drawable1 = getResources().getDrawable(
                        R.drawable.ocoffee_search_pic1);
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(),
                        drawable1.getMinimumHeight());
                party_info_jg.setCompoundDrawables(null, null, drawable1, null);

                getAndShowData("1");
                break;

            case R.id.party_info_xp:

                party_info_rq.setTextColor(Color.parseColor("#333333"));
                party_info_xl.setTextColor(Color.parseColor("#333333"));
                party_info_xp.setTextColor(Color.parseColor("#FF3E47"));
                party_info_jg.setTextColor(Color.parseColor("#333333"));

                Drawable drawable11 = getResources().getDrawable(
                        R.drawable.ocoffee_search_pic1);
                drawable11.setBounds(0, 0, drawable11.getMinimumWidth(),
                        drawable11.getMinimumHeight());
                party_info_jg.setCompoundDrawables(null, null, drawable11, null);

                getAndShowData("2");
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {

            party_info_rq.setTextColor(Color.parseColor("#333333"));
            party_info_xl.setTextColor(Color.parseColor("#333333"));
            party_info_xp.setTextColor(Color.parseColor("#333333"));
            party_info_jg.setTextColor(Color.parseColor("#FF3E47"));

            Drawable drawable = getResources().getDrawable(
                    R.drawable.ocoffee_search_pic2);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            party_info_jg.setCompoundDrawables(null, null, drawable, null);

            getAndShowData("4");
        } else {

            party_info_rq.setTextColor(Color.parseColor("#333333"));
            party_info_xl.setTextColor(Color.parseColor("#333333"));
            party_info_xp.setTextColor(Color.parseColor("#333333"));
            party_info_jg.setTextColor(Color.parseColor("#FF3E47"));

            Drawable drawable = getResources().getDrawable(
                    R.drawable.ocoffee_search_pic3);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                    drawable.getMinimumHeight());
            party_info_jg.setCompoundDrawables(null, null, drawable, null);

            getAndShowData("3");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.CID,
                "");
        Intent intent = new Intent(PartyInfoActivity.this,
                ProductsActivity.class);
        PreferenceUtils.getInstance().SetSettingString(
                PreferencesConfig.PRODUCT_NUMBER,
                bean.getList().get(position).getProduct_number());
        startActivity(intent);
    }
}
